// _form/cpt/submit-form/index.js
const { c0, $wx } = getApp()
Component({
    behaviors: ['wx://form-field-group'],
    options: {
        virtualHost: true,
        multipleSlots: true
    },
    properties: {
        id: { type:String, value: 'form' },
        data: { type: Array, value: [] },
        className: String,
        defaultValue: null,
        labelStyle:String,
        labelTextStyle:String,
        labelWidth: Number,
        labelFontSize: { type:Number, value: 30 },
        fontSize: { type:Number, value: 30 },
        gap: { type:Number, value: 30 },
        y:{ type:Boolean, value:true },
        h:{ type:Boolean, value:true },
        gapX: { type:Number, value: 10 },
        gapY: { type:Number,value:20 },
        subText: {type:String, value:'提交'},
        resetText: {type:String, value:'重置'},
        reset:{ type:Boolean, value: false },
        confirm:null,
        clear:Boolean,
        changeError: Boolean,
        popErr: Boolean,
        msgErr: Boolean,
        msgErr: Boolean,
        disabled: Boolean,
        loading:Boolean,
        autoSubmit:Boolean
    },
    data: {
        c0
    },
    methods: {
        form(){ return this.selectComponent('#' + this.data.id) },
        onSubmit({ detail: { value } }){
            this.form().submit(value).then(data=>this.triggerEvent('submit', data))
        },
        onChange({ detail }){
            this.triggerEvent('change', detail)
        },
        getValue( name ){
            return this.form().getValue(name)
        },
        reset(){
            this.form().reset()
        },
        clear(){
            this.form().clear()
        },
        submit(){
            const { confirm, subText } = this.data
            if(confirm){
                $wx.modal({
                    c: confirm === true ? '确认' + subText + '?' : confirm,
                    ok: () => {
                        const { data:{form} } = this.form()
                        this.form().submit(form).then(data=>this.triggerEvent('submit', data))
                    }
                })
            }
        }
    }
})
